## Figure 5: Effect of Donations at Time t on Lobbying at Time t+k: -------------

## Instructions ----------------------------------------------------------------
# Step 1: Adjust DATA_DIR to where Data is is located
# Step 2: Adjust OUT_DIR to where Output folder is
# Step 3: Run entire script


## setup -----------------------------------------------------------------------
# clean slate
rm(list = ls())
# Need to install to read/use PanelEstimate Objects
# devtools::install_github("insongkim/PanelMatch",ref = "big_data_checks")

## library
library(dplyr)
library(data.table)
library(PanelMatch)

# set directory
MAIN_DIR <- "C:/Users/js.egb/Dropbox/campaign-lobby-paper/replication_package"

# set data folder
DATA_DIR <-  paste(MAIN_DIR, "/data/fig5/", sep = "")

# set output folder
OUT_DIR <- paste(MAIN_DIR, "/output/", sep = "")



#####################################################################
## Committees, Cosponsor, Sponsor
#####################################################################


## PAC Donations

yaxis_limits <- c(-0.05, 0.175) # Y Axis
xaxis_limits <- c(0.5,4.5) # X Axis
legend_font_size <- 1.25 # Font Size of Legend (if any)
point_size <- 7 # size of points

## Start Plotting Device
pdf(file = file.path(OUT_DIR, "figure_5.pdf"), # comm_cospon_spon.pdf
    height = 7, width = 18)

## Plotting Parameters
par(font.lab = 1.8, cex.lab = 3, cex.axis = 2,
    cex.main = 3, mar = c(8,11.5,5,0.5), las=1, mfrow=c(1,3))

## PAC
load(file.path(DATA_DIR, "EstObj_manuf_ann_senate_maha_PacDon_CommLob"))
pm_sum <- summary(PM.lobby)$summary # get summary DF from PE object

plot(PM.lobby, main = "",
     ylim = yaxis_limits, xlim = xaxis_limits,
     ylab = "", xlab = "", col = "black")

title(ylab = "Estimated Effects of Donation on\nProbability to Lobby", line = 6.2)

## mtext(side = 2, "Estimated Effects of Donation on Lobbying",
##       line = 4, cex=2)

mtext(side = 3, "Bill Committee Assignment",
      line = 1, cex=2)

## mtext(side = 1, "Years relative to the timing of donation",
##       line = 3.3, cex=2.2)

## plot again with larger dots and CIs
points(c(1:length(pm_sum[,1])), pm_sum[,1],
       lwd = point_size, col = "black", pch = 19, cex=1.2)
for (j in 1:length(pm_sum[,1])) {
  arrows(x0 = j, x1 = j, y0 =  pm_sum[j,4], y1 = pm_sum[j,3],
         length = 0,lwd = 2, col = "black")
}

rm(pm_sum, PM.lobby)


## CEO
load(file.path(DATA_DIR, "EstObj_manuf_ann_senate_maha_CeoDon_CommLob"))
pm_sum <- summary(PM.lobby)$summary # get summary DF from PE object

## plot again with larger dots and CIs
points(c(1:length(pm_sum[,1]) + 0.12), pm_sum[,1],
       lwd = point_size, col = "blue", pch = 17, cex=2.2)
for (j in 1:length(pm_sum[,1])) {
  arrows(x0 = j + 0.12, x1 = j+0.12, y0 =  pm_sum[j,4], y1 = pm_sum[j,3],
         length = 0,lwd = 2, col = "blue")
}

rm(pm_sum, PM.lobby)



## Employee
load(file.path(DATA_DIR, "EstObj_manuf_ann_senate_maha_EmpDon_CommLob"))
pm_sum <- summary(PM.lobby)$summary # get summary DF from PE object

## plot again with larger dots and CIs
points(c(1:length(pm_sum[,1]) + 0.24), pm_sum[,1],
       lwd = point_size, col = "red", pch = 15, cex=2.2)
for (j in 1:length(pm_sum[,1])) {
  arrows(x0 = j + 0.24, x1 = j+0.24, y0 =  pm_sum[j,4], y1 = pm_sum[j,3],
         length = 0,lwd = 2, col = "red")
}


rm(pm_sum, PM.lobby)

legend("topleft", legend=c("PAC", "CEO", "Employee"),
       col=c("black", "blue", "red"), bty="n",
       pch = c(19, 17, 15),
       lty=1, cex=2)

#####################################################################
## center panel: Cosponsorship
#####################################################################


## PAC
load(file.path(DATA_DIR, "EstObj_manuf_ann_senate_maha_PacDon_CoSpLob"))
pm_sum <- summary(PM.lobby)$summary # get summary DF from PE object


par(mar = c(8,8,5,4))
plot(PM.lobby, main = "",
     ylim = yaxis_limits, xlim = xaxis_limits,
     ylab = "", xlab = "", col = "black")

## mtext(side = 2, "Estimated Effects of Donation on Lobbying",
##       line = 4, cex=2)

mtext(side = 1, "Years relative to the timing of donation",
      line = 6, cex=2.2)

mtext(side = 3, "Bill Co-Sponsorship",
      line = 1, cex=2)

## plot again with larger dots and CIs
points(c(1:length(pm_sum[,1])), pm_sum[,1],
       lwd = point_size, col = "black", pch = 19, cex=1.2)
for (j in 1:length(pm_sum[,1])) {
  arrows(x0 = j, x1 = j, y0 =  pm_sum[j,4], y1 = pm_sum[j,3],
         length = 0,lwd = 2, col = "black")
}

rm(pm_sum, PM.lobby)


## CEO
load(file.path(DATA_DIR, "EstObj_manuf_ann_senate_maha_CeoDon_CoSpLob"))
pm_sum <- summary(PM.lobby)$summary # get summary DF from PE object

## plot again with larger dots and CIs
points(c(1:length(pm_sum[,1]) + 0.12), pm_sum[,1],
       lwd = point_size, col = "blue", pch = 17, cex=2.2)
for (j in 1:length(pm_sum[,1])) {
  arrows(x0 = j + 0.12, x1 = j+0.12, y0 =  pm_sum[j,4], y1 = pm_sum[j,3],
         length = 0,lwd = 2, col = "blue")
}

rm(pm_sum, PM.lobby)



## Employee
load(file.path(DATA_DIR, "EstObj_manuf_ann_senate_maha_EmpDon_CoSpLob"))
pm_sum <- summary(PM.lobby)$summary # get summary DF from PE object

## plot again with larger dots and CIs
points(c(1:length(pm_sum[,1]) + 0.24), pm_sum[,1],
       lwd = point_size, col = "red", pch = 15, cex=2.2)
for (j in 1:length(pm_sum[,1])) {
  arrows(x0 = j + 0.24, x1 = j+0.24, y0 =  pm_sum[j,4], y1 = pm_sum[j,3],
         length = 0,lwd = 2, col = "red")
}


rm(pm_sum, PM.lobby)


#####################################################################
## Right panel: Spnosorship
#####################################################################

## PAC
load(file.path(DATA_DIR, "EstObj_manuf_ann_senate_maha_PacDon_SpLob"))
pm_sum <- summary(PM.lobby)$summary # get summary DF from PE object


par(mar = c(8,4,5,8))
plot(PM.lobby, main = "",
     ylim = yaxis_limits, xlim = xaxis_limits,
     ylab = "", xlab = "", col = "black")


mtext(side = 3, "Bill Sponsorship",
      line = 1, cex=2)


## plot again with larger dots and CIs
points(c(1:length(pm_sum[,1])), pm_sum[,1],
       lwd = point_size, col = "black", pch = 19, cex=1.2)
for (j in 1:length(pm_sum[,1])) {
  arrows(x0 = j, x1 = j, y0 =  pm_sum[j,4], y1 = pm_sum[j,3],
         length = 0,lwd = 2, col = "black")
}

rm(pm_sum, PM.lobby)


## CEO
load(file.path(DATA_DIR, "EstObj_manuf_ann_senate_maha_CeoDon_SpLob"))
pm_sum <- summary(PM.lobby)$summary # get summary DF from PE object

## plot again with larger dots and CIs
points(c(1:length(pm_sum[,1]) + 0.12), pm_sum[,1],
       lwd = point_size, col = "blue", pch = 17, cex=2.2)
for (j in 1:length(pm_sum[,1])) {
  arrows(x0 = j + 0.12, x1 = j+0.12, y0 =  pm_sum[j,4], y1 = pm_sum[j,3],
         length = 0,lwd = 2, col = "blue")
}

rm(pm_sum, PM.lobby)



## Employee
load(file.path(DATA_DIR, "EstObj_manuf_ann_senate_maha_EmpDon_SpLob"))
pm_sum <- summary(PM.lobby)$summary # get summary DF from PE object

## plot again with larger dots and CIs
points(c(1:length(pm_sum[,1]) + 0.24), pm_sum[,1],
       lwd = point_size, col = "red", pch = 15, cex=2.2)
for (j in 1:length(pm_sum[,1])) {
  arrows(x0 = j + 0.24, x1 = j+0.24, y0 =  pm_sum[j,4], y1 = pm_sum[j,3],
         length = 0,lwd = 2, col = "red")
}


rm(pm_sum, PM.lobby)


dev.off()




